package com.huwaiwai.disruptor.demo01;

import com.lmax.disruptor.RingBuffer;
import lombok.AllArgsConstructor;

import java.nio.ByteBuffer;

/**
 * 事件生成源-事件生产者
 * Created by huweiwei on 2017/4/11.
 */
@AllArgsConstructor
public class LongEventProducer {

    private final RingBuffer<LongEvent> ringBuffer;

    public void onData(ByteBuffer bb) {
        long sequence = ringBuffer.next();
        try {
            LongEvent event = ringBuffer.get(sequence);
            event.setValue(bb.getLong(0));
        } finally {
            //发布事件
            ringBuffer.publish(sequence);
        }
    }
}
